package leetcode.medium.q17;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        System.out.println(new Solution().letterCombinations("234"));
    }
}
class Solution {
    char[][]chars=new char[][]{
            {'-','-','-'},
            {'a','b','c'},
            {'d','e','f'},
            {'g','h','i'},
            {'j','k','l'},
            {'m','n','o'},
            {'p','q','r','s'},
            {'t','u','v'},
            {'w','x','y','z'}
    };
    public List<String> letterCombinations(String digits) {

        if (digits==null||digits.length()==0) return new ArrayList<>();
        List<String> result = new ArrayList<>();
        List<StringBuilder> stringBuilders = appendNext(0, digits);
        for (int i = 0; i < stringBuilders.size(); i++) {
            result.add(stringBuilders.get(i).toString());
        }
        return result;

    }
    public List<StringBuilder> appendNext(Integer index,String digits){
        if (index == digits.length()-1) {
            ArrayList<StringBuilder> stringBuilders = new ArrayList<>();
            int pos = digits.charAt(index)-49;
            for (int i = 0; i < chars[pos].length; i++) {
                stringBuilders.add(new StringBuilder(String.valueOf(chars[pos][i])));
            }
            return stringBuilders;
        }
        int pos = digits.charAt(index)-49;
        List<StringBuilder> res =new ArrayList<>();
        for (int j = 0; j < chars[pos].length; j++) {
            List<StringBuilder> stringBuilders = appendNext(index + 1, digits);
            for (int i = 0; i < stringBuilders.size(); i++) {
                res.add(new StringBuilder(String.valueOf(chars[pos][j]))
                        .append(stringBuilders.get(i)));
            }
        }
        return res;

    }
}
